import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Leetcode1805 {

    public static void main(String[] args) {
        System.out.println(numDifferentIntegers1("a123bc34d8ef34"));
        System.out.println(numDifferentIntegers1("leet1234code234"));
        System.out.println(numDifferentIntegers1("a1b01c001"));
        System.out.println(numDifferentIntegers1("0a0"));
        System.out.println(numDifferentIntegers1("a"));
    }

    public static int numDifferentIntegers1(String word) {
        return Arrays.stream(word.replaceAll("[a-z]+", " ").trim().split(" "))
                .filter(s -> !s.isBlank())
                .map(BigInteger::new)
                .collect(Collectors.toSet())
                .size();
    }

    public static int numDifferentIntegers(String word) {
        char[] chars = word.toCharArray();
        Set<String> count = new HashSet<>();
        StringBuilder stringBuilder = new StringBuilder();

        loop:
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] < '0' || chars[i] > '9') {
                continue;
            }

            while (chars[i] == '0') {
                i++;
                if (i >= chars.length) {
                    count.add("0");
                    break loop;
                }
            }

            if (chars[i] < '0' || chars[i] > '9') {
                count.add("0");
                continue;
            }

            while (i < chars.length && chars[i] >= '0' && chars[i] <= '9') {
                stringBuilder.append(chars[i]);
                i++;
            }

            count.add(stringBuilder.toString());
            stringBuilder.setLength(0);
        }

        return count.size();
    }
}
